﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using GestionePolveriLib.Facades;
using GestionePolveriLib.BL;
using GestionePolveriLib.BL.Filters;
using GestionePolveriLib.Utilities;

namespace GestionePolveri2.Filters
{
    public partial class AmministrazioneFilter : AmministrazioneFilterBase
    {
        public LottoLoadingModes LoadingMode 
        {
            set
            {
                this.rbAssociate.Checked = false; 
                this.rbNonAssociate.Checked = false;
                this.rbTutte.Checked = false;

                switch (value)
                {
                    case LottoLoadingModes.NotAssociated:
                        this.rbNonAssociate.Checked = true;
                        break;
                    case LottoLoadingModes.Associated:
                        this.rbAssociate.Checked = true;
                        break;
                    case LottoLoadingModes.All:
                        this.rbTutte.Checked = true;
                        break;
                    default:
                        break;
                }
            }
        }

        public AmministrazioneFilter()
        {
            InitializeComponent();

            if (!this.DesignMode)
            {
                RefreshData();

                this.FromDate = DateTime.Now;
                this.ToDate = DateTime.Now; 
            }
        }

        private void LoadPolveri()
        {
            FilterComboBoxHelper.SetupPolveriComboBox(this.cmbPolveri);
        }

        private void LoadFornitori()
        {
            FilterComboBoxHelper.SetupFornitoriComboBox(this.cmbFornitori);
        }

        public void LoadLotti()
        {
            FilterComboBoxHelper.SetupLottiComboBox(this.cmbLotti);
        }

        protected override void OnResetFilter(EventArgs e)
        {
            base.OnResetFilter(e);

            FilterBag.CodiceLotto = null;
            FilterBag.CodicePolvere = null;
            FilterBag.NumDDT = null;
            FilterBag.Fornitore = null;
            FilterBag.LoadingMode = LottoLoadingModes.NotAssociated;

            this.cmbLotti.SelectedIndex = 0;
            this.cmbPolveri.SelectedIndex = 0;
            this.cmbFornitori.SelectedIndex = 0;

            this.FromDate = DateTime.Now;
            this.ToDate = DateTime.Now;

            this.rbNonAssociate.Checked = true;
        }

        protected override void OnExecuteFilter(EventArgs e)
        {
            FilterBag.UseDates = this.UseDatesFilter;

            FilterBag.PeriodFrom = this.FromDate;
            FilterBag.PeriodTo = this.ToDate;
            if (this.cmbLotti.SelectedItem == null)
                this.cmbLotti.SelectedIndex = 0;
            if (this.cmbPolveri.SelectedItem == null)
                this.cmbPolveri.SelectedIndex = 0;
            if (this.cmbFornitori.SelectedItem == null)
                this.cmbFornitori.SelectedIndex = 0;

            FilterBag.CodiceLotto = (((Lotto)this.cmbLotti.SelectedItem).IDLotto == -1 ? null : ((Lotto)this.cmbLotti.SelectedItem).CodLotto);
            FilterBag.CodicePolvere = (((Polvere)this.cmbPolveri.SelectedItem).ID == -1 ? null : ((Polvere)this.cmbPolveri.SelectedItem).CodicePolvere);
            FilterBag.NumDDT = (string.IsNullOrWhiteSpace(this.txtNumDDT.Text) ? null : this.txtNumDDT.Text);
            FilterBag.Fornitore = (((Fornitore)this.cmbFornitori.SelectedItem).ID == -1 ? null : ((Fornitore)this.cmbFornitori.SelectedItem).Descrizione);
            FilterBag.LoadingMode = (this.rbAssociate.Checked ? LottoLoadingModes.Associated : 
                                                                  (this.rbNonAssociate.Checked ? LottoLoadingModes.NotAssociated :
                                                                                                 LottoLoadingModes.All));

            if (
                (this.cmbLotti.SelectedIndex == 0) &&
                (this.cmbPolveri.SelectedIndex == 0) &&
                (this.cmbFornitori.SelectedIndex == 0) &&
                (FilterBag.CodiceLotto == null) &&
                (FilterBag.CodicePolvere == null) &&
                (FilterBag.Fornitore == null) &&
                (FilterBag.LoadingMode == LottoLoadingModes.NotAssociated) &&
                (FilterBag.UseDates == false)
              )
            {
                OnResetFilter(e);
                this.OnFilterButtonClick();
            }
            else
            {
                base.OnExecuteFilter(e);
            }

            //base.OnExecuteFilter(e);
        }

        public override void RefreshData()
        {
            LoadPolveri();
            LoadFornitori();
            LoadLotti();
        }
    }

    public class AmministrazioneFilterBase : FilterContainerBase2<AmministrazioneFilterBag> { }
}
